// pages/palyball/index.js
import {request} from "../../request/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    history_data: [],
    laugh_story: '',
    index: 0,
    animationData: {},
    animationData2: {}
  },
  laugh_data: [],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.get_historyEvent()
    this.get_laughEvent()
  },
  // 获取历史上今日事件回调函数
  get_historyEvent(){
    this.setData({history_data: []})
    // 聚合数据那年今日key
    const key = '2e932a732b70b2e766f53a6011f268ae'
    const url = 'https://v.juhe.cn/todayOnhistory/queryEvent.php'
    const date = new Date()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const date_str = month + '/' + day
    // 暂时不捕获异常
    request({url, data: {key, date: date_str}}).then((res) => {
      const history_data = res.data.result.slice(0, 3)
      this.setData({history_data})
    })
  },
  // 一则笑话回调函数
  get_laughEvent(){
    // 聚合数据笑话大全key
    this.setData({laugh_story: ''})
    const key = '7e6f356e93b8f8d70f04c62efed6a108'
    const url = 'https://v.juhe.cn/joke/content/list.php'
    const time_stamp = parseInt(Date.now() / 1000)
    request({url, data: {key, sort: 'desc', time: time_stamp}}).then((res) => {
      const laugh_story = res.data.result.data[0].content
      this.laugh_data = res.data.result.data
      this.setData({laugh_story})
    })
  },
  // 更新一则笑话
  handleRefresh(){
    // 涮新按钮动画
    let animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease'
    })
    animation.rotateZ(-180).step()
    // 重置旋转角度，回归起点
    animation.rotateZ(180).step()
    this.setData({animationData: animation.export()})
    // 指针步进 +1
    let index =  ++this.data.index
    const laugh_story = this.laugh_data[index].content
    this.setData({index, laugh_story})
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // 重新分享方法
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    return {
      title: "开心一刻",
      path: "/pages/today/index"
    }
  },
  /**
   * 用户点击分享朋友圈
   */
  onShareTimeline(){}
})